home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / strategy / xsok-1.000 / xsok-1 / xsok-1.01 / src / showscore.c < prev    next >
C/C++ Source or Header  |  1995-11-25  |  3KB  |  112 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdarg.h>
  5. #include <time.h>
  6.  
  7. #define MAGICH    0x741d    /* magic of xsok version 1 highscore file */
  8.  
  9. static void portable_to_internal(long *args, unsigned char *p, int num) {
  10.     do {
  11.     int j;
  12.     *args = 0;
  13.     for (j = 0; j < 4; ++j)
  14.         *args += (long)p[3-j] << (j << 3);
  15.     ++args;
  16.     p += 4;
  17.     } while (--num);
  18. }
  19. int highscore[300];
  20. static void ReadHighscores(const char *levelfile) {
  21.     FILE *fp;
  22.     char filename[256], base_name[32], *s;
  23.     int i;
  24.  
  25.     memset(highscore, 0, sizeof(highscore));
  26.     for (i = 100; i < 300; ++i)
  27.         highscore[i] = 0x7fffffff;
  28.  
  29.     strcpy(base_name, levelfile);
  30.     if (!(s = strchr(base_name, '.')))
  31.     return;
  32.     strcpy(s+1, "score");
  33.     sprintf(filename, "%s/%s", XSOKSAVE, base_name);
  34.     if ((fp = fopen(filename, "rb"))) {
  35.     long p[300];
  36.     unsigned char ss[1200];
  37.     fread(ss, 4, 300, fp);
  38.     portable_to_internal(p, ss, 300);    
  39.     for (i = 0; i < 300; ++i)
  40.         highscore[i] = p[i];
  41.     fclose(fp);
  42.     }
  43.     highscore[0] = MAGICH;
  44. }
  45.  
  46.  
  47. static int brief = 1;
  48. #define NARGS 16
  49.  
  50. static void checkfile(const char *filename) {
  51.     FILE *fp;
  52.     long p[NARGS];
  53.     unsigned char s[256];
  54.     char type[10], *z;
  55.     int namelen, level;
  56.  
  57.     if (!(fp = fopen(filename, "rb")))
  58.     return;
  59.     fread(s, 4, NARGS, fp);
  60.     portable_to_internal(p, s, NARGS);
  61.     if (p[0] != 0x741b)
  62.     return;
  63.     fread(type, 1, 8, fp);
  64.     level = p[6];
  65.     type[8] = '\0';
  66.     namelen = getc(fp);
  67.     fread(s, 1, namelen, fp);
  68.     s[namelen] = '\0';
  69.  
  70.     if (strrchr(filename, '/'))
  71.     filename = strrchr(filename, '/') + 1;
  72.     ReadHighscores(filename);
  73.  
  74.     z = ctime(&p[4]);
  75.     if (strchr(z, '\n'))
  76.     *strchr(z, '\n') = '\0';
  77.  
  78.     if (!brief) {
  79.     printf("%-14s %-7s Level %2d:  Score: %5ld, Pushes: %4ld, Moves: %4ld\n",
  80.            filename, type, level, p[1], p[2], p[3]);
  81. #if 0
  82.     if (!p[5])    /* unsolved */
  83.         printf("\n");
  84.     else
  85. #endif
  86.         printf("saved %s by %s\n", z, s);
  87.     } else {
  88.     int c1, c2, c3;
  89.     c1 = p[5] && highscore[level]     == p[1] ? '*' : ' ';
  90.     c2 = p[5] && highscore[level+200] == p[2] ? '*' : ' ';
  91.     c3 = p[5] && highscore[level+100] == p[3] ? '*' : ' ';
  92.     printf("%2d %5ld%c %4ld%c %4ld%c  %s\n", level, p[1], c1, p[2], c2, p[3], c3, s);
  93.     }
  94. }
  95.  
  96. int main(int argc, char *argv[]) {
  97.     int i;
  98.     if (argc == 1) {
  99.     fprintf(stderr, "usage: showscore [-b] (savefiles)\n");
  100.     exit(1);
  101.     }
  102.     i = 1;
  103.     if (!strcmp(argv[i], "-b")) {
  104.     brief = 0;
  105.     ++i;
  106.     } else
  107.     printf("Lv Score Pushes Moves  Player\n");
  108.     for (; i < argc; ++i)
  109.     checkfile(argv[i]);
  110.     return 0;
  111. }
  112.